package com.shanzhu.beadhouse.controller;

import cn.hutool.json.JSONUtil;
import com.shanzhu.beadhouse.entity.base.Result;
import com.shanzhu.beadhouse.entity.query.*;
import com.shanzhu.beadhouse.service.IServicePurchaseService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

public class ServicePurchaseControllerTest {

    @InjectMocks
    private ServicePurchaseController controller;

    @Mock
    private IServicePurchaseService servicePurchaseService;

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.openMocks(this);
    }

    // ------------------ addPurchase ------------------
    @Test
    public void testAddPurchase_Success() {
        AddPurchaseFromListQuery query = new AddPurchaseFromListQuery();
        Result result = Result.success();
        when(servicePurchaseService.addPurchase(query)).thenReturn(result);

        Result response = controller.addPurchase(query, "token");

        assertEquals(200, response.getCode());
        verify(servicePurchaseService, times(1)).addPurchase(query);
    }

    @Test
    public void testAddPurchase_Failure() {
        AddPurchaseFromListQuery query = new AddPurchaseFromListQuery();
        Result result = Result.error(500,"error");
        when(servicePurchaseService.addPurchase(query)).thenReturn(result);

        Result response = controller.addPurchase(query, "token");

        assertEquals(500, response.getCode());
        verify(servicePurchaseService, times(1)).addPurchase(query);
    }

    // ------------------ pagePurchaseByKeys ------------------
    @Test
    public void testPagePurchaseByKeys_Success() {
        PagePurchaseByKeysQuery query = new PagePurchaseByKeysQuery();
        Result result = Result.success();
        when(servicePurchaseService.pagePurchaseByKeys(query)).thenReturn(result);

        Result response = controller.pagePurchaseByKeys(query, "token");

        assertEquals(200, response.getCode());
        verify(servicePurchaseService, times(1)).pagePurchaseByKeys(query);
    }

    @Test
    public void testPagePurchaseByKeys_Failure() {
        PagePurchaseByKeysQuery query = new PagePurchaseByKeysQuery();
        Result result = Result.error(500,"error");
        when(servicePurchaseService.pagePurchaseByKeys(query)).thenReturn(result);

        Result response = controller.pagePurchaseByKeys(query, "token");

        assertEquals(500, response.getCode());
        verify(servicePurchaseService, times(1)).pagePurchaseByKeys(query);
    }

    // ------------------ deletePurchaseByKeys ------------------
    @Test
    public void testDeletePurchaseByKeys_Success() {
        DeletePurchaseQuery query = new DeletePurchaseQuery();
        Result result = Result.success();
        when(servicePurchaseService.deletePurchase(query)).thenReturn(result);

        Result response = controller.deletePurchaseByKeys(query, "token");

        assertEquals(200, response.getCode());
        verify(servicePurchaseService, times(1)).deletePurchase(query);
    }

    @Test
    public void testDeletePurchaseByKeys_Failure() {
        DeletePurchaseQuery query = new DeletePurchaseQuery();
        Result result = Result.error(500,"error");
        when(servicePurchaseService.deletePurchase(query)).thenReturn(result);

        Result response = controller.deletePurchaseByKeys(query, "token");

        assertEquals(500, response.getCode());
        verify(servicePurchaseService, times(1)).deletePurchase(query);
    }

    // ------------------ getPurchase ------------------
    @Test
    public void testGetPurchase_Success() {
        GetPurchaseByIdQuery query = new GetPurchaseByIdQuery();
        Result result = Result.success();
        when(servicePurchaseService.getPurchase(query)).thenReturn(result);

        Result response = controller.getPurchase(query, "token");

        assertEquals(200, response.getCode());
        verify(servicePurchaseService, times(1)).getPurchase(query);
    }

    @Test
    public void testGetPurchase_Failure() {
        GetPurchaseByIdQuery query = new GetPurchaseByIdQuery();
        Result result = Result.error(500,"error");
        when(servicePurchaseService.getPurchase(query)).thenReturn(result);

        Result response = controller.getPurchase(query, "token");

        assertEquals(500, response.getCode());
        verify(servicePurchaseService, times(1)).getPurchase(query);
    }

    // ------------------ resetHoldById ------------------
    @Test
    public void testResetHoldById_Success() {
        ResetHoldQuery query = new ResetHoldQuery();
        Result result = Result.success();
        when(servicePurchaseService.resetHoldById(query)).thenReturn(result);

        Result response = controller.resetHoldById(query, "token");

        assertEquals(200, response.getCode());
        verify(servicePurchaseService, times(1)).resetHoldById(query);
    }

    @Test
    public void testResetHoldById_Failure() {
        ResetHoldQuery query = new ResetHoldQuery();
        Result result = Result.error(500,"error");
        when(servicePurchaseService.resetHoldById(query)).thenReturn(result);

        Result response = controller.resetHoldById(query, "token");

        assertEquals(500, response.getCode());
        verify(servicePurchaseService, times(1)).resetHoldById(query);
    }
}
